/*
Copyright 2025 Codenotary Inc. All rights reserved.

SPDX-License-Identifier: BUSL-1.1
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    https://mariadb.com/bsl11/

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package tbtree

import (
	"bytes"
	"encoding/json"
	"testing"

	"github.com/stretchr/testify/require"
)

func consistencyCheck(t *testing.T, tbtree *TBtree, n node) {
	switch n := n.(type) {
	case *innerNode:
		// All nodes must be within the range of this node
		for _, sub := range n.nodes {
			require.True(t, bytes.Compare(n.minKey(), sub.minKey()) <= 0)
		}

		// All nodes must have sorted non-verlapping ranges
		for i := 1; i < len(n.nodes); i++ {
			require.True(t, bytes.Compare(n.nodes[i-1].minKey(), n.nodes[i].minKey()) < 0)
		}

		// Consistency for child nodes
		for _, sub := range n.nodes {
			consistencyCheck(t, tbtree, sub)
		}

	case *leafNode:
		// All values must be within the range of this node
		for _, v := range n.values {
			require.True(t, bytes.Compare(n.minKey(), v.key) <= 0)
		}

		// All values must be sorted by keys
		for i := 1; i < len(n.values); i++ {
			require.True(t, bytes.Compare(n.values[i-1].key, n.values[i].key) < 0)
		}

	case *nodeRef:
		// check if reference is consistent with the node
		sub, err := tbtree.nodeAt(n.off, true)
		require.NoError(t, err)
		require.True(t, bytes.Equal(n._minKey, sub.minKey()))

		consistencyCheck(t, tbtree, sub)

	default:
		require.Fail(t, "unknown node type")
	}
}

func TestConsistencyFailure(t *testing.T) {

	dataset := []string{
		`[{"K":"AkNUTC5EQVRBQkFTRS4AAAAB","V":"AAAACgEAAAAAAAAAmKw+rNqZUNaZOr1kBNPS74JCKQ2US/6sA8G3QTmDMiEAAAAA"}]`,
		`[{"K":"AGttZDp2Y24uZGVtby5iNDAzNDIzYzYyNWUyMzA3ODhkOGZmYzBkNTVlN2RhYTdmNjU0ZDU2ZTEwZjA1NDVjZT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kODQ2ZTQ4ZTYwNjQ1MTBiYzg1OTBlY2M2ZjYzNjUwZWY4M2Y0Y2I3NzFhMmMwNTE0ZGNmZQAAAAAAAAAG","V":"AAAAAAAAAAAAAAAA47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFUAAAAA"},
		  {"K":"AQAAAAAAAAAaZ2l0aHViLmNvbS9kb2NrZXIvZ28tdW5pdHMAAAAAAAAAAAAAAAAAAABKAHZjbi5kZW1vLjBhNjI4ZGE1ZjUyZDg0NmU0OGU2MDY0NTEwYmM4NTkwZWNjNmY2MzY1MGVmODNmNGNiNzcxYTJjMDUxNGRjZmUAAAAAAAAAAA==","V":"AAAAAAAAAAAAAAAA47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFUAAAAA"},
		  {"K":"AF9JVEVNLkxFREdFUi1OQU1FLnZjbi5kZW1vLjBhNjI4ZGE1ZjUyZDg0NmU0OGU2MDY0NTEwYmM4NTkwZWNjNmY2MzY1MGVmODNmNGNiNzcxYTJjMDUxNGRjZmU=","V":"AAAADAEAAAAAAAl88HpkwEmpRXStn2PtPpvaG5L3vSc9FFmYLNIVcPnWG2YAAAAA"}]`,
	}

	t.Run("no flush", func(t *testing.T) {
		tbtree, err := Open(t.TempDir(), DefaultOptions())
		require.NoError(t, err)
		defer tbtree.Close()

		for _, d := range dataset {
			kvs := []*KVT{}
			require.NoError(t, json.Unmarshal([]byte(d), &kvs))

			for _, kv := range kvs {
				err := tbtree.BulkInsert([]*KVT{kv})
				require.NoError(t, err)

				consistencyCheck(t, tbtree, tbtree.root)
			}
		}
	})

	t.Run("with flush", func(t *testing.T) {
		tbtree, err := Open(t.TempDir(), DefaultOptions())
		require.NoError(t, err)
		defer tbtree.Close()

		for _, d := range dataset {
			kvs := []*KVT{}
			require.NoError(t, json.Unmarshal([]byte(d), &kvs))

			for _, kv := range kvs {
				tbtree.Flush()
				err := tbtree.BulkInsert([]*KVT{kv})
				require.NoError(t, err)

				consistencyCheck(t, tbtree, tbtree.root)
			}
		}
	})
}
